﻿using System;
using System.Collections.Generic;

namespace HEFramework
{
    public static class UIComponentSystem
    {
        [EntitySystem]
        public class UIComponentAwakeSystem : AwakeSystem<UIComponent>
        {
            protected override void Awake(UIComponent _self)
            {
            }
        }


        [EntitySystem]
        public class UIComponentDestroySystem : DestroySystem<UIComponent>
        {
            protected override void Destroy(UIComponent _self)
            {
                _self.AllUIToCache();
                _self.DestroyCacheUI();
            }
        }

        public static IUIWindowBehaviour PushUISyn(this UIComponent _self, eUIFlag _flag)
        {
            IUIWindowBehaviour ui;
            _self.UIWindowBehaviours.TryGetValue(_flag, out ui);
            if (ui == null)
            {
                ui = UILoader.Instance.PushUISyn(_flag.ToString());
                _self.UIWindowBehaviours.Add(_flag, ui);
            }

            // 在显示之前，再次确认UI没有被销毁
            if (ui != null && !ui.Equals(null) && ui.State != -1)
            {
                ui.Show();
            }
            else
            {
                ui = UILoader.Instance.PushUISyn(_flag.ToString());
                _self.UIWindowBehaviours[_flag] = ui;
            }

            return ui;
        }

        public static void PushUIAsync(this UIComponent _self, eUIFlag _flag, Action<IUIWindowBehaviour> _callback)
        {
            IUIWindowBehaviour ui;
            _self.UIWindowBehaviours.TryGetValue(_flag, out ui);
            if (ui == null)
            {
                UILoader.Instance.PushUIAsync(_flag.ToString(), _ =>
                {
                    ui = _;
                    _self.UIWindowBehaviours.Add(_flag, ui);
                    ui.Show();
                    _callback(ui);
                });
            }
            else
            {
                // 在显示之前，再次确认UI没有被销毁
                if (ui != null && !ui.Equals(null) && ui.State != -1)
                {
                    ui.Show();
                    _callback(ui);
                }
                else
                {
                    UILoader.Instance.PushUIAsync(_flag.ToString(), _ =>
                    {
                        ui = _;
                        _self.UIWindowBehaviours[_flag] = ui;
                        ui.Show();
                        _callback(ui);
                    });
                }
            }
        }


        public static void ShowAllUI(this UIComponent _self)
        {
            foreach (var ui in _self.UIWindowBehaviours.Values)
            {
                ui.Show();
            }
        }

        public static void HideAllUI(this UIComponent _self)
        {
            foreach (var ui in _self.UIWindowBehaviours.Values)
            {
                ui.Hide();
            }
        }

        public static void AllUIToCache(this UIComponent _self)
        {
            foreach (var ui in _self.UIWindowBehaviours.Values)
            {
                ui.Pop();
            }

            _self.UIWindowBehaviours.Clear();
        }

        public static void DestroyCacheUI(this UIComponent _self)
        {
            UILoader.Instance.DestroyCacheUI();
        }
    }
}